import Vue from 'vue';
import Vuex from 'vuex';
import api from '~/api/api.js';
import en from 'element-ui/lib/locale/lang/en.js';
import cn from 'element-ui/lib/locale/lang/zh-CN.js';
import locale from 'element-ui/lib/locale';
import langZhCN from '~/locale/lang/zh-CN.js';
import langEn from '~/locale/lang/en.js';

let login = window.localStorage.getItem('login');

const store = new Vuex.Store({
	state: {
		guest: login ? false : true,
		loading: false,
		carts: [],
		language: 'zh-CN',
		lang: langZhCN,
		// access_token: access_token
	},
	mutations: {
		login (state) {
			state.guest = false;
			console.log(state.guest);
		},
		logout (state) {
			state.guest = true;
		},
		loading (state, value) {
			state.loading = value;
		},
		loadCart (state, value) {
			state.carts = value ? value : [];
		},
		// 购物车全选
		cartAllSelect (state, value) {
			state.carts.forEach(function (item) {
				item.checked = value;
			})
		},
		// 设置购物车数量
		setCartQty (state, data) {
			for (let k in state.carts) {
				let item = state.carts[k];
				if (item.id == data.cart.id) {
					item.qty = data.value;
					break;
				}
			}
		},
		setCartFavorite (state, cart) {
			for (let k in state.carts) {
				let item = state.carts[k];
				if (item.id == cart.id) {
					item.favorite = !item.favorite;
					break;
				}
			}
		},
		removeCart (state, cart) {
			for (let k in state.carts) {
				let item = state.carts[k];
				if (item.id == cart.id) {
					state.carts.splice(k, 1);
					break;
				}
			}	
		},
		// 设置语言
		setLanguage (state, language) {
			state.language = language
			if (language == 'zh-CN') {
				locale.use(cn);
				state.lang = langZhCN;
			} else if (language == 'en') {
				locale.use(en);
				state.lang = langEn;
			}
		}
	},
	actions: {
		// 获取购物车
		getCart (context) {
			return api.get('/cart/index')
			.then(response => {
				context.commit('loadCart', response.data);
			});
		},
		// 改变购物车数量
		changeCartQty (context, data) {
			return api.post('/cart/change_qty/'+data.cart.id, {
				qty: data.value
			}).then(function () {
				context.commit('setCartQty', data);
			});
		},
		// 删除购物车
		removeCart (context, cart) {
			return api.post('/cart/delete', {
				id: cart.id
			})
			.then(response => {
				context.commit('removeCart', cart);
			});
		},
		// 购物车移至收藏夹
		favoriteCartReversal (context, cart) {
			let promise;
			if (!cart.favorite) {
				promise = api.get('/favorite/add_product/'+cart.product_id)	
			} else {
				promise = api.get('/favorite/remove_product/'+cart.product_id);
			}
			return promise.then(() => {
				context.commit('setCartFavorite', cart);
			});
		},
		// 登录
		login (context, user) {
			return api.post('/login/', {
				phone: user.phone,
				password: user.password
			})
			.then(function (response) {
				context.commit('login');
				return response;
			});
		},
		checkUser (context) {
			return api.get('/user/info')
			.then(response => {
				context.commit('login');
				return response;
			})
			.catch(error => {
				if(error.response.status == 401) {
					context.commit('logout');
				}

				return error;
			});
		}
	}
});

export default store;